<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>用户管理 | {$system_name}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="icon" type="image/png" href="{_ADMIN_STATIC_}/i/favicon.png">
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/amazeui.min.css" />
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/admin.css">
    <link rel="stylesheet" href="{_ADMIN_STATIC_}/css/app.css">

</head>

<body data-type="index">
    <!-- 头部导航栏 -->
    {include file='public/header'}
    <!-- 左侧侧边栏 -->
    {include file='public/side'}
    <!-- 中间内容开始 -->

    <!-- 标题 -->
    <div class="tpl-content-page-title">
        {$system_name} -- 用户管理
    </div>
    <!-- 面包屑导航 -->
    <ol class="am-breadcrumb">
        <li><i class="am-icon-home"></i>用户管理</li>
    </ol>
    <!-- 提示信息 -->
    <div class="tpl-content-scope">
        <div class="note note-info">
            <h3> 用户管理
                <span class="close" data-close="note"></span>
            </h3>
            <p>用户管理</p>
            <p><span class="label label-danger">提示:</span>
                前端用户的管理，与后端用户无关
            </p>
        </div>

        <div class="tpl-portlet-components">
            <div class="am-btn-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default am-btn-success" onclick="user_add()"><span
                        class="am-icon-plus"></span>
                    新增单个用户</button>
            </div>

            <div class="am-btn-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default am-btn-success" onclick="user_import()">
                    <span class="am-icon-chevron-right"></span>
                    用户批量导入</button>
                <input type="file" id="file_upload" name="file_upload" style="display: none;"
                    accept="text/csv,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
            </div>

            <div class="am-btn-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default am-btn-success" onclick="download()"><span
                        class="am-icon-download"></span>
                    模板下载</button>
            </div>

            <table class="am-table am-table-striped am-table-hover table-main">
                <thead>
                    <tr>
                        <th class="table-id">#</th>
                        <th class="table-title">用户账号</th>
                        <th class="table-author">用户名称</th>
                        <th class="table-type">办公室电话</th>
                        <th class="table-title">手机</th>
                        <th class="table-date">创建时间</th>
                        <th class="table-set" style="width: 220px;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {volist name="user_list" id="row" empty="$empty"}
                    <tr>
                        <td>{$row.id}</td>
                        <td>{$row.account}</td>
                        <td>{$row.name}</td>
                        <td>{$row.tel}</td>
                        <td>{$row.phone}</td>
                        <td>{$row.create_time | date='Y-m-d H:i'}</td>
                        <td>
                            <div class="am-btn-toolbar">
                                <div class="am-btn-group am-btn-group-xs">
                                    <button class="am-btn am-btn-default am-btn-xs am-text-secondary am-hide-sm-only"
                                        onclick="user_role('{$row.id}')"><span class="am-icon-pencil-square-o"></span>
                                        授权</button>
                                    <button class="am-btn am-btn-default am-btn-xs am-text-secondary"
                                        onclick="user_edit('{$row.id}')"><span class="am-icon-pencil-square-o"></span>
                                        编辑</button>
                                    <button class="am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only"
                                        onclick="user_del('{$row.id}')"><span class="am-icon-trash-o"></span>
                                        删除</button>
                                </div>
                            </div>
                        </td>
                    </tr>
                    {/volist}
                </tbody>
            </table>
            <!-- 加载分页组件 -->
            {include file="public/page"}

        </div>
    </div>


    <!-- 中间内容结束 -->
    <!-- 底部 -->
    <!-- {include file="public/footer"} -->

    <script src="{_ADMIN_STATIC_}/lib/jquery/jquery.js"></script>
    <script src="{_ADMIN_STATIC_}/js/amazeui.min.js"></script>
    <script src="{_ADMIN_STATIC_}/js/app.js"></script>
    <script src="{_ADMIN_STATIC_}/lib/layer/layer.js"></script>
    <script>
        // 添加一个用户
        function user_add() {
            return layer.open({
                type: 2,
                title: '添加一个用户',
                skin: 'layui-layer-rim', //加上边框
                area: ['420px', '380px'], //宽高
                content: "{:url('User/user_add')}",
                end: function (e) {
                    location.reload();
                }
            });
        }

        // 编辑用户
        function user_edit($uid) {
            redirect_url("{:url('User/user_edit')}?uid=" + $uid);
        }

        // 删除用户
        function user_del($id) {
            layer.prompt({ title: '如果要删除该用户，请输入[立即删除]', formType: 0 }, function (text, index) {
                layer.close(index);
                if (text != "立即删除") {
                    return layer.msg('口令输入错误，无法删除', { icon: 2, time: 1500 });
                }

                $.ajax({
                    url: "{:url('User/user_del')}",
                    data: {
                        'uid': $id
                    },
                    type: "POST",
                    dataType: "JSON",
                    success: function (res) {
                        if (res.code == 0) {
                            return layer.msg(res.msg, { icon: 1, time: 900 }, function () {
                                location.reload();
                            });
                        } else {
                            return layer.msg(res.msg, { icon: 2, time: 1500 });
                        }
                    }
                });
            });
        }

        // 授权
        function user_role($uid) {
            redirect_url("{:url('User/user_role')}?uid=" + $uid);
        }

        // 下载模板
        function download() {

            $.ajax({
                url: "{:url('User/user_import_tmp_download')}",
                data: {},
                type: "POST",
                dataType: "JSON",
                success: function (res) {
                    if (!res.status) {
                        return layer.msg('禁止下载模板', { icon: 2 });
                    } else {
                        window.location.href = res.url;
                    }
                }
            });

        }

        // 打开文件选择框
        function user_import() {
            $('#file_upload').click();
        }

        // 上传
        $('#file_upload').change(function () {
            var files = $('#file_upload').prop('files');
            var data = new FormData();
            data.append('user_data', files[0]);

            $.ajax({
                url: "{:url('User/user_import')}",
                data: data,
                type: "POST",
                dataType: "JSON",
                contentType: false,
                processData: false,
                success: function (res) {
                    console.log(res);
                    if (res.code == 0) {
                        return layer.msg(res.msg, { icon: 1, time: 900 }, function () {
                            location.reload();
                        });
                    } else {
                        return layer.msg(res.msg + '\n' + res.data.toString(), { icon: 2, time: 20000, btn: ['知道了'] }, function () {
                            location.reload();
                        });
                    }
                },
                error: function (res) {
                    console.log(res.responseText);
                }
            });

            $('#file_upload').val('');
        });

        // 重定向
        function redirect_url(url) {
            window.location.href = url;
        }

    </script>
</body>

</html>